<template>
  <div>
    <el-container>
      <el-header class="cate_mana_header">
        <el-input
          placeholder="请输入Tag名称"
          v-model="tagName"
          style="width: 200px;"
        >
        </el-input>
        <el-button
          type="primary"
          size="medium"
          style="margin-left: 10px"
          @click="addNewCate"
          >新增Tag</el-button
        >
      </el-header>
      <el-main class="cate_mana_main">
        <el-table
          ref="multipleTable"
          :data="tags"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          v-loading="loading"
        >
          <el-table-column type="selection" width="55" align="left">
          </el-table-column>
          <el-table-column label="编号" prop="id" width="120" align="left">
          </el-table-column>
          <el-table-column
            label="Tag名称"
            prop="tagName"
            width="120"
            align="left"
          >
          </el-table-column>
          <el-table-column
            label="文章数"
            prop="articleCount"
            width="120"
            align="left"
          >
          </el-table-column>

          <el-table-column label="操作" align="left">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button
          type="danger"
          :disabled="this.selItems.length == 0"
          style="margin-top: 10px;width: 100px;"
          @click="deleteAll"
          v-if="this.tags.length > 0"
          >批量删除
        </el-button>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import { get, post, del } from "../utils/api";
export default {
  data() {
    return {
      tagName: "",
      selItems: [],
      tags: [],
      loading: false
    };
  },
  mounted() {
    this.loading = true;
    this.refresh();
  },
  methods: {
    addNewCate() {
      this.loading = true;
      post("/admin/tags/", { tagName: this.tagName }).then(resp => {
        if (resp.code == 0) {
          this.$message({ type: "success", message: resp.message });
          this.tagName = "";
          this.refresh();
        }
        this.loading = false;
      });
    },
    deleteAll() {
      var _this = this;
      this.$confirm("确认删除这 " + this.selItems.length + " 条数据?", "提示", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(() => {
          var selItems = _this.selItems;
          var ids = "";
          for (var i = 0; i < selItems.length; i++) {
            ids += selItems[i].id + ",";
          }
          _this.deleteCate(ids.substring(0, ids.length - 1));
        })
        .catch(() => {
          //取消
          _this.loading = false;
        });
    },
    handleSelectionChange(val) {
      this.selItems = val;
    },
    handleDelete(index, row) {
      let _this = this;
      this.$confirm("确认删除 " + row.tagName + " ?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          _this.deleteCate(row.id);
        })
        .catch(() => {
          //取消
          _this.loading = false;
        });
    },
    deleteCate(ids) {
      this.loading = true;
      //删除
      del("/admin/tags/" + ids)
        .then(resp => {
          if (resp.code == 0) {
            this.$message({
              type: "success",
              message: resp.message
            });
          } else {
            this.$message({
              type: "error",
              message: resp.message
            });
          }
          this.refresh();
        })
        .catch(err => {
          this.$message({
            type: "error",
            message: err
          });
          this.refresh();
        });
    },
    refresh() {
      get("/admin/tags/").then(resp => {
        this.tags = resp.data;
        this.loading = false;
      });
    }
  }
};
</script>

<style>
.cate_mana_header {
  background-color: #ececec;
  margin-top: 20px;
  padding-left: 5px;
  display: flex;
  justify-content: flex-start;
}

.cate_mana_main {
  /*justify-content: flex-start;*/
  display: flex;
  flex-direction: column;
  padding-left: 5px;
  background-color: #ececec;
  margin-top: 20px;
  padding-top: 10px;
}
</style>
